//
//  LVLoadingView.m
//  Live
//
//  Created by 熙文 张 on 16/3/29.
//  Copyright © 2016年 Heller. All rights reserved.
//

#import "LVLoadingView.h"
#import "LVSpringIndicator.h"
#import "UIView+YYAdd.h"
#import "YYCGUtilities.h"
#import "YYAnimatedImageView.h"
#import "LVLoadingAnimation.h"
#import "UIColor+YYAdd.h"
#import "LKSequenceAnimation.h"
#import "BNDOfficeDocKit.h"

#define kAnimationDuration 1.3f

@interface LVLoadingView()

//@property (strong, nonatomic) LKSequenceAnimation *loadingAnimation;

@property (nonatomic, strong) LKSequenceAnimation  *loadingAnimation;
@property (strong, nonatomic) UIView              *animationView;

@end

@implementation LVLoadingView

+ (LVLoadingView *)findLoadingView:(UIView*)view
{
    LVLoadingView *hud = nil;
    for(UIView *v in view.subviews)
    {
        if ([v isKindOfClass:[LVLoadingView class]]) {
            hud = (LVLoadingView *)v;
            [hud setHidden:NO];
            break;
        }
    }
    
    return hud;
}

#pragma mark - 显示LoadingView

+ (LVLoadingView *)showLoadingViewAddedTo:(UIView *)view frame:(CGRect)bounds coverHeader:(BOOL)coverHeader
{
    LVLoadingView *hud = [self findLoadingView:view];
    
    if (!hud) {
        UITableView *tableView = nil;
        if ([view isKindOfClass:[UITableView class]]) {
            tableView = (UITableView *)view;
            tableView.scrollEnabled = NO;
        }
        
        if([tableView tableHeaderView] != nil && !coverHeader) {
            hud = [[LVLoadingView alloc] initWithFrame:CGRectMake(0,
                                                                  tableView.tableHeaderView.height - tableView.contentInset.top,
                                                                  view.width,
                                                                  view.height - tableView.tableHeaderView.height)];
        }
        else {
            hud = [[LVLoadingView alloc] initWithFrame:bounds];
        }
        [view addSubview:hud];
        [view bringSubviewToFront:hud];
    }
    
    if([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *tmp = (UIScrollView*)view;
        tmp.scrollEnabled = NO;
    }else {
        view.userInteractionEnabled = NO;
    }
    
    return hud;
}

+ (LVLoadingView *)showLoadingViewAddedTo:(UIView *)view frame:(CGRect)bounds
{
    return [LVLoadingView showLoadingViewAddedTo:view frame:bounds coverHeader:YES];
}

+ (LVLoadingView *)showLoadingViewAddedTo:(UIView *)view
{
    return [self showLoadingViewAddedTo:view frame:view.bounds];
}

#pragma mark - 开始LoadingView动画

+ (void)startAnimationTo:(UIView* )view
{
    LVLoadingView *hud = [self findLoadingView:view];
    if (hud.isClearColor)
    {
        [hud setBackgroundColor:[UIColor clearColor]];
    }
    else
    {
        [hud setBackgroundColor:UIColorWithHex(0xf5f5f5)];
    }
    [hud setStartTime:CFAbsoluteTimeGetCurrent()];
    
    if (hud) {
        [hud beginAnimated];
    }
}

#pragma mark - 隐藏LoadingView

+ (void)hideLoadingViewForView:(UIView *)view
{
    LVLoadingView *hud = [self findLoadingView:view];
    [hud setEndTime:CFAbsoluteTimeGetCurrent()];
    
    //确保加载动画展示200ms后消失
    NSTimeInterval interval = hud.endTime - hud.startTime;
    NSTimeInterval delayInterval = 0.0f;
    if (interval < 0.3) {
        delayInterval = 0.3 - interval;
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (hud) {
            [hud stopAnimated];
        }
    });
    
    if([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *tmp = (UIScrollView *)view;
        tmp.scrollEnabled = YES;
    }else {
        view.userInteractionEnabled = YES;
    }
}

+ (void)hideLoadingViewForView:(UIView *)view delay:(NSTimeInterval)delay
{
    LVLoadingView *hud = [self findLoadingView:view];
    [hud setEndTime:CFAbsoluteTimeGetCurrent()];
    
    //确保加载动画展示200ms后消失
    NSTimeInterval interval = hud.endTime - hud.startTime;
    if (interval < 0.2) {
        delay = 0.2 - interval;
    }
    
    if (hud)
    {
        if(delay == 0)
        {
            [hud stopAnimated];
        }
        else
        {
            double delayInSeconds = delay;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [hud stopAnimated];
            });
        }
        hud = nil;
    }
    
    if([view isKindOfClass:[UITableView class]])
    {
        UITableView *tmp = (UITableView *)view;
        tmp.scrollEnabled = YES;
    }
}

+ (void)hideLoadingViewImmediatelyForView:(UIView *)view
{
    LVLoadingView *hud = [self findLoadingView:view];
    [hud setEndTime:CFAbsoluteTimeGetCurrent()];
    [hud stopAnimated];
    if([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *tmp = (UIScrollView *)view;
        tmp.scrollEnabled = YES;
    }else {
        view.userInteractionEnabled = YES;
    }
}

#pragma mark - Init

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (nil != self)
    {
        self.backgroundColor = [UIColor whiteColor];
        //UIColorWithHex(0xf5f5f5);
        
//        _loadingAnimation = [LOTAnimationView animationNamed:@"loading.json"];
//        [_loadingAnimation  setLoopAnimation:YES];
//
//        _loadingAnimation.frame = CGRectMake((self.width-100)/2, (self.height-100)/2, 100, 100);
//        [self addSubview:_loadingAnimation];
//        [_loadingAnimation mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.mas_equalTo(self);
//            make.size.mas_equalTo(CGSizeMake(120, 120));
//        }];
        
        
        _loadingAnimation = [[LKSequenceAnimation alloc] init];
        _loadingAnimation.lineType = LKSequenceAnimationSmall;
        [_loadingAnimation initImageViewUI:self];
    }
    
    return self;
}

#pragma mark - Animation Control

- (void)beginAnimated
{
    _starting = YES;
//    [_loadingAnimation ];
    [_loadingAnimation startAnimationHaveBeginAnimation];
}

- (void)stopAnimated
{
    _starting = NO;
    
    self.alpha = 0.0f;

    [_imageView stopAnimating];
    [_loadingAnimation stopAnimation];
//    [_loadingAnimation S];
    [self removeFromSuperview];
}

//暂停旋转动画
- (void)pauseRunning
{
    [_imageView stopAnimating];
}

//恢复旋转动画
-(void)resumeRunning
{
    [self beginAnimated];
}

@end
